package de.bensoft.jrail.client.ui.locodriver;

import java.util.List;

import javafx.beans.binding.Bindings;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.control.ToggleButton;
import javafx.scene.layout.HBox;
import javafx.util.converter.NumberStringConverter;

import javax.inject.Inject;

import de.bensoft.jrail.business.entity.Locomotive;
import de.bensoft.jrail.business.service.LocomotiveService;
import de.bensoft.jrail.client.com.TrainCommunicator;
import de.bensoft.jrail.client.com.TrainInstance;
import de.bensoft.jrail.client.common.JavaFXController;

@JavaFXController(fxml="LocoDriver.fxml")
public class LocoDriverController {

	//********************************************************************************
	// Injects
	//********************************************************************************
	@Inject
	LocomotiveService locoService;
	
	@Inject
	TrainCommunicator communicator;

	
	//********************************************************************************
	// EXT
	//********************************************************************************
	public void setActiveLocomotive(Locomotive l) {
			
		if(cbLoco.getItems().contains(l)) {
			cbLoco.getSelectionModel().select(l);
		}
		
		if(l == null)
			return;
		
	}
	
	//********************************************************************************
	// FXML
	//********************************************************************************
	@FXML
	private ToggleButton tglLight;
	
	@FXML
	private Slider slSpeed;
	
	@FXML
	private Label lblSpeedValue;
	
	@FXML
	private ComboBox<Locomotive> cbLoco;
	
	@FXML
	private HBox hbControls;
	
	@FXML
	private Button btnDirection;
	
	@FXML
	private void onStop() {
		if(ti != null)
			ti.speedProperty().set(0);
	}
	
	@FXML
	private void onDirection() {
		if(ti == null)
			return;
		
		if(ti.driveForwardProperty().get()) {
			btnDirection.setText("<");
			ti.driveForwardProperty().set(false);
		} else {
			btnDirection.setText(">");
			ti.driveForwardProperty().set(true);
		}
	}
	
	@FXML
	private void initialize() {
		initGUI();
	}
	
	//********************************************************************************
	// DATA
	//********************************************************************************
	private void refreshData() {
		List<Locomotive> lst = locoService.getAllLocomotives();
		ObservableList<Locomotive> lsto = FXCollections.observableList(lst);
		cbLoco.setItems(lsto);				
	}
	
	
	//********************************************************************************
	// GUI
	//********************************************************************************
	private Locomotive currentLOCO;
	
	private void initGUI() {
		hbControls.setDisable(true);
		
		refreshData();
		
		cbLoco.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Locomotive>() {
			public void changed(ObservableValue<? extends Locomotive> arg0, Locomotive arg1, Locomotive arg2) {
				currentLOCO = arg2;
				bindFormToDTO();
			}	
		});
		
	}
	
	private TrainInstance ti;
	private void bindFormToDTO() {
		if(currentLOCO == null)
			return;
		
		if(currentLOCO.getSpeedSteps() != null) {
			slSpeed.setMin(0);
			slSpeed.setMax(currentLOCO.getSpeedSteps());
		} else {
			slSpeed.setMin(0);
			slSpeed.setMax(128);
		}
		
		if(ti != null) {
			slSpeed.valueProperty().unbindBidirectional(ti.speedProperty());
			lblSpeedValue.textProperty().unbindBidirectional(ti.speedProperty());
			tglLight.selectedProperty().unbindBidirectional(ti.lightProperty());
			
		}
		
		ti = communicator.getTrainInstanceByLocmotive(currentLOCO);	
		
		slSpeed.valueProperty().bindBidirectional(ti.speedProperty());
		Bindings.bindBidirectional(lblSpeedValue.textProperty(), ti.speedProperty(), new NumberStringConverter());
		
		tglLight.selectedProperty().bindBidirectional(ti.lightProperty());
			
		hbControls.setDisable(false);
	}
}
